# 基本捕获语法
# try:
#      可能出现异常的代码
# except:
#      如果出现异常执行的代码

try:
    f = open("D:/Users/李健奇/OneDrive/桌面/Python/基础/异常/abc.txt", "r", encoding="UTF-8")
except:
    print("出现异常了，因为文件不存在，我将open改为w模式打开")
    f = open("D:/Users/李健奇/OneDrive/桌面/Python/基础/异常/abc.txt", "w", encoding="UTF-8")
finally:
    print("我是finally，有没有异常我都要执行")
    f.close()
"""
# 捕获指定异常
try:
    print(a)
except NameError as e:
    print("变量a没有被定义")
    print(e)

# 捕获多个异常
try:
    # print(b)
    1 / 0
except (NameError,ZeroDivisionError) as e:
    print("出现了变量未定义异常 或 除0异常")
"""

# 捕获全部异常
# try:
#     f = open("D:/def.txt","r",encoding="UTF-8")
#     # print(c)
#     # 1 / 0
# except Exception as e:
#     print("出现异常了")